﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using _1.StudentsFromGroup2.Model;

namespace _1.StudentsFromGroup2
{
    class Program
    {
        static Group gMath = new Group("Math"); 
        static Group gBio = new Group("Biology");
        static Group gChem = new Group("Chemistry");
        static Group gInfo = new Group("Cool");


        static Student[] students = 
            { 
                new Student(){ FirstName = "Vesko", LastName = "Kolev", FN = "000006001", Telephone = "0288 123 123", Email = "e@g.c", Marks = new List<int>(){2,  2}, Group = gMath},
                new Student(){ FirstName = "Peshko", LastName = "Marchev", FN = "000006002", Telephone = "028853 123", Email = "e@g.c", Marks = new List<int>(){6,6 }, Group = gMath},
                new Student(){ FirstName = "Toshko", LastName = "Marinov", FN = "000006003", Telephone = "0288 123 123", Email = "e@g.c", Marks = new List<int>(){6, 3}, Group = gBio},
                new Student(){ FirstName = "Kristina", LastName = "Kolev", FN = "000007004", Telephone = "0888 123 123", Email = "e@g.c", Marks = new List<int>(){3, 2}, Group = gBio},
                new Student(){ FirstName = "Daniel", LastName = "Marinov", FN = "000007005", Telephone = "0888 123 123", Email = "e1@g.c", Marks = new List<int>(){ 2, 3, 2}, Group = gBio},
                new Student(){ FirstName = "Ivana", LastName = "Kolev", FN = "000007006", Telephone = "0888 123 123", Email = "e@abv.bg", Marks = new List<int>(){ 6, 2, 2, 2, 3, 2, 2, 3, 2}, Group = gChem},
                new Student(){ FirstName = "Malinka", LastName = "Kolev", FN = "000008007", Telephone = "0888 123 123", Email = "e2@abv.bg", Marks = new List<int>(){ }, Group = gMath},
                new Student(){ FirstName = "Iva", LastName = "Kolev", FN = "000006008", Telephone = "0288 123 123", Email = "e3@abv.bg", Marks = new List<int>(){6,6, 6, 2, 2, 2, 3, 2, 2, 3, 2},Group = gChem},
                new Student(){ FirstName = "Chavdar", LastName = "Marinov", FN = "000006009", Telephone = "0888 123 123", Email = "e4@abv.bg", Marks = new List<int>(){}, Group = gChem},
                new Student(){ FirstName = "Vladi", LastName = "Marchev", FN = "000005010", Telephone = "0888 123 123", Email = "e5@abv.bg", Marks = new List<int>(){6,6, 6, 2, 2, 2, 3, 2, 2, 3, 2}, Group = gInfo},
                new Student(){ FirstName = "Boiko", LastName = "Kolev", FN = "000005011", Telephone = "0888 123 123", Email = "e6@abv.bg", Marks = new List<int>(){6,6, 6, 2, 2, 2, 3, 2, 2, 3, 2}, Group = gInfo},
                new Student(){ FirstName = "Vesko", LastName = "Kolev", FN = "000005012", Telephone = "0888 123 123", Email = "e7@g.c", Marks = new List<int>(){6,2, 2, 3, 2, 2, 3, 2}, Group = gInfo},
            }; 

        static void Main(string[] args)
        {
            var group2 = from s in students
                         where s.Group.GroupNumber == 2
                         select s;

            Print(group2);

            group2 = students.Where(s => s.Group.GroupNumber == 2);

            Console.WriteLine();
            Print(group2);

            var studentsWithEmailsAtAbv = from s in students
                                          where s.Email.EndsWith("abv.bg")
                                          select s;

            Console.WriteLine();
            Console.WriteLine("students with email at abv.bg");
            Print(studentsWithEmailsAtAbv);

            var studentsWithTelephonesInSofia = from s in students
                                                where TelephoneIsInSofia(s.Telephone)
                                                select s;

            Console.WriteLine();
            Console.WriteLine("students with telephone in sofia");
            Print(studentsWithTelephonesInSofia);

            var studentsWithAtLeastOne6 = from s in students
                                                where s.Marks.Contains(6)
                                                select s;

            Console.WriteLine();
            Console.WriteLine("students with at least one 6 as a mark");
            Print(studentsWithAtLeastOne6);

            var studentsWithExactly2Marks = from s in students
                                            where s.Marks.Count == 2
                                            select new { FullName = s.FirstName + " " + s.LastName, Marks = s.Marks };

            Console.WriteLine();
            Console.WriteLine("students with exactly 2 marks");
            Print(studentsWithExactly2Marks);

            var marksOfStudentsEnrolledIn2006 = students.Where(s => StudentEnrollementYear(s) == 2006).SelectMany(s => s.Marks);

            Console.WriteLine();
            Console.WriteLine("marks Of Students Enrolled In 2006");
            Print(marksOfStudentsEnrolledIn2006);


            var studentsFromMath = from s in students
                                   where s.Group.DepartmentName == "Math"
                                   select s;

            Console.WriteLine();
            Console.WriteLine("students from group math");
            Print(studentsFromMath);
        }

        private static int StudentEnrollementYear(Student s)
        {
            string yearAsString = s.FN.Substring(4, 2);
            int year = Convert.ToInt32(yearAsString);
            
            return 2000 + year;
        }

        private static bool TelephoneIsInSofia(string tel)
        {
            // use regular expression ?
            return tel.StartsWith("02");
        }

        private static void Print(IEnumerable<Student> students)
        {
            foreach (var s in students)
            {
                Console.WriteLine("{0} {1}, FN:{2}, G:{3}", s.FirstName, s.LastName, s.FN, s.Group.GroupNumber);
            }
        }

        private static void Print(IEnumerable<int> numbers)
        {
            foreach (var n in numbers)
            {
                Console.Write("{0} ", n);                
            }

            Console.WriteLine();
        }

        private static void Print(IEnumerable<object> objects)
        {
            foreach (var o in objects)
            {
                Console.WriteLine(o);
            }
        }

        
    }
}
